package com.ruoyi.api.kehu;

import com.ruoyi.system.utils.TokenUtil;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.config.ServerConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.system.domain.JiajiaActivateCity;
import com.ruoyi.system.service.AdminUploadService;
import com.ruoyi.system.service.IJiajiaCityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api/common")
@Api(tags = "通用")
public class ApiCommonController {

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private IJiajiaCityService cityService;

    @Autowired
    private AdminUploadService adminUploadService;


    /**
     * 通用上传请求（单个）
     */
    @PostMapping("/up/single")
    @ResponseBody
    public AjaxResult uploadFile(MultipartFile file) throws Exception
    {
        try
        {


            Map<String, String> stringStringMap = adminUploadService.uploadFile(file);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", stringStringMap.get("fileName"));
            ajax.put("url", stringStringMap.get("url"));
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 多文件上传
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/up/more")
    @ResponseBody
    public AjaxResult uploadFileToMore(MultipartHttpServletRequest request) throws Exception
    {
        try
        {
            AjaxResult ajax = AjaxResult.success();

            MultiValueMap<String, MultipartFile> filesMap = request.getMultiFileMap();
            for(Map.Entry<String, List<MultipartFile>> entry:filesMap.entrySet()){
                // 上传并返回新文件名称
                Map<String, String> stringStringMap = adminUploadService.uploadFile( entry.getValue().get(0));
                ajax.put(entry.getKey()+"_url", stringStringMap.get("url"));
            }

            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "上传图片接口")
    @PostMapping(value = "/upload")
    public AjaxResult upload(@ApiParam(value = "图片文件", required = true) MultipartHttpServletRequest request){
        MultiValueMap<String, MultipartFile> filesMap = request.getMultiFileMap();
        try {
            String filePath = RuoYiConfig.getUploadPath();
            for (String key : filesMap.keySet()) {
                List<MultipartFile> fileList = filesMap.get(key);
                for (MultipartFile fl : fileList) {
                    if (fl.getSize()>0) {
                        String fileName = FileUploadUtils.upload(filePath, fl);
                        String url = serverConfig.getUrl() + fileName;
                        return AjaxResult.success(url);
                    }
                }
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.error("上传失败");
    }

    @ApiOperation(value = "获取地区列表")
    @PostMapping("/getCityList")
    public AjaxResult getCityList(){

        JiajiaActivateCity city = new JiajiaActivateCity();
        List<JiajiaActivateCity> cityList = cityService.selectJiajiaCityList(city);

        return AjaxResult.success(cityList);
    }

    @ApiOperation(value = "获取地址信息")
    @PostMapping("/getAddreseByLocation")
    public AjaxResult getAddreseByLocation(){
        return AjaxResult.success(TokenUtil.getAddreseByLocation());
    }

}
